//
//  DYWTopView.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/20.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class DYWTopView: UIView {

    // MARK:--重载构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = CornflowerBlue
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:--model 属性
    var model:DYWWeiboModel? {
        didSet {
            // 设头像的图片
            if let urlString = model?.user?.avatar_large {
            iconView?.sd_setImageWithURL(NSURL(string: urlString))
            } else {
                return
            }
            // 设置名称
            userLabel.text = model?.user?.screen_name
            
            if model?.user?.verified == true {
                userLabel.textColor = UIColor.redColor()
            } else {
                userLabel.textColor = UIColor.blackColor()
            }
            // TODO:-- 后期完善
            // 设置日期属性
            timeLabel.text = "13:13"
            // 设置来源
            sourceLabel.text = "淋王星客户端"
            // 设置正文
            textLabel.text = model?.text
            // 设置 vip 头像
            vipIcon.image = model?.user?.mbrank_img
            // 设置 认证头像
            verified_imgView.image = model?.user?.verified_img
            // 设置配图视图的 urls 属性
            weiBoImagesView.imgUrls = model?.imgUrls
            
        }
    }
    
    // MARK:--重写 layOutSubViews 
    override func layoutSubviews() {
        super.layoutSubviews()
        setUpUI()
    }
    
    /// 懒加载头像
    lazy var iconView:UIImageView! = UIImageView()
    
    /// 懒加载头像
    lazy var userLabel:UILabel! = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Left
        label.textColor = UIColor.blackColor()
        label.sizeToFit()
        return label
    }()
    
    /// 懒加载时间 label 
    lazy var timeLabel:UILabel! = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.textAlignment = NSTextAlignment.Left
        label.textColor = UIColor.orangeColor()
        label.sizeToFit()
        return label
    }()
    
    /// 懒加载来源 label 
    lazy var sourceLabel:UILabel = UILabel(fontSize: 12, alignment: NSTextAlignment.Left, contentColor: UIColor.darkGrayColor(), lineCount: 1)
    
    /// 懒加载正文 label
    lazy var textLabel:UILabel = UILabel(fontSize: 14, alignment: NSTextAlignment.Left, contentColor: UIColor.blackColor(), lineCount: 0)
    
    /// 懒加载会员图形 (等级)
    lazy var vipIcon:UIImageView = UIImageView()
    
    /// 懒加载用户 v
    lazy var verified_imgView:UIImageView = UIImageView()
    
    /// 懒加载配图视图
    lazy var weiBoImagesView:DYWWeiBoImageView = DYWWeiBoImageView()
    

    
    /// 设置 UI 
    private func setUpUI () {
        addSubview(iconView)
        addSubview(verified_imgView)
        addSubview(userLabel)
        addSubview(vipIcon)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(weiBoImagesView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self.snp_leading).offset(commonMargin)
            make.top.equalTo(self.snp_top).offset(commonMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        verified_imgView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        userLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(commonMargin)
            make.top.equalTo(iconView.snp_top)
        }
        
        vipIcon.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userLabel.snp_top)
            make.left.equalTo(userLabel.snp_right).offset(commonMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(commonMargin)
            make.top.equalTo(userLabel.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(commonMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(commonMargin)
            make.leading.equalTo(iconView.snp_leading)
            make.trailing.equalTo(self.snp_trailing).offset(-commonMargin)
        }
        
        weiBoImagesView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(commonMargin)
            make.top.equalTo(textLabel.snp_bottom).offset(commonMargin)
//            make.size.equalTo(CGSizeMake(40, 40))
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(weiBoImagesView.snp_bottom).offset(commonMargin)
        }
        
        iconView.layer.cornerRadius = 4
        iconView.layer.masksToBounds = true
        iconView.layer.borderColor = IndianRed.CGColor
        iconView.layer.borderWidth = 2

    }
}
